package com.kanshu.ksgb.fastread.doudou.module.reader.page;



import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by newbiechen on 17-5-29.
 * 网络页面加载器
 */

public class NetPageLoader extends PageLoader {
    private static final String TAG = "PageFactory";

    public NetPageLoader(PageView pageView, String bookId) {
        super(pageView, bookId);
    }

    @Override
    public void refreshChapterList() {

    }

    @Override
    protected BufferedReader getChapterReader(TxtChapter chapter) throws Exception {
        File file = CacheManager.getInstance().getChapterFile(chapter.bookId, chapter.chapterIndex);
        if (!file.exists()) return null;
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        return br;
    }

    @Override
    protected boolean hasChapterData(String bookId, int chapterIndex) {
        return CacheManager.getInstance().getChapterFile(bookId, chapterIndex) != null;
    }

    // 装载上一章节的内容
    @Override
    boolean parsePrevChapter() {
        boolean isRight = super.parsePrevChapter();
        if (mStatus == STATUS_FINISH) {
            loadPrevChapter();
        } else if (mStatus == STATUS_LOADING) {
            loadCurrentChapter();
        }
        return isRight;
    }

    // 装载当前章内容。
    @Override
    boolean parseCurChapter() {
        boolean isRight = super.parseCurChapter();

        if (mStatus == STATUS_LOADING) {
            loadCurrentChapter();
        }
        return isRight;
    }

    // 装载下一章节的内容
    @Override
    boolean parseNextChapter() {
        boolean isRight = super.parseNextChapter();

        if (mStatus == STATUS_FINISH) {
            loadNextChapter();
        } else if (mStatus == STATUS_LOADING) {
            loadCurrentChapter();
        }

        return isRight;
    }

    /**
     * 加载当前页的前面两个章节
     */
    private void loadPrevChapter() {
        if (mPageChangeListener != null) {
            int end = mCurChapterPos;
            int begin = end - 2;
            if (begin <= 0) {
                begin = 1;
            }

            //requestChapters(begin, end);
        }
    }

    /**
     * 加载前一页，当前页，后一页。
     */
    private void loadCurrentChapter() {
        if (mPageChangeListener != null) {
            if (mCurChapterPos > mChapterCount) {
                return;
            }
            mPageChangeListener.requestChapter(mCurChapterPos);
        }
    }

    /**
     * 加载当前页的后两个章节
     */
    private void loadNextChapter() {
        if (mPageChangeListener != null) {

            // 提示加载后两章
            int begin = mCurChapterPos + 1;
            int end = begin + 1;

            // 判断是否大于最后一章
            if (begin > mChapterCount) {
                // 如果下一章超出目录了，就没有必要加载了
                return;
            }

            if (end > mChapterCount) {
                end = mChapterCount;
            }
            requestChapters(begin, end);
        }
    }

    private void requestChapters(int start, int end) {
        // 检验输入值
        if (start <= 0) {
            start = 1;
        }
        if (end > mChapterCount && mChapterCount != 0
                && end >= start) {
            end = mChapterCount - 1;
        }
        List<TxtChapter> chapters = new ArrayList<>();
        // 过滤，哪些数据已经加载了
        for (int i = start; i <= end; ++i) {
            TxtChapter txtChapter = getTxtChapter(i);
            if (!hasChapterData(mBookId, i) && txtChapter != null) {
                chapters.add(txtChapter);
            }
            if (txtChapter != null)
                txtChapter.recycle();
        }

        if (!chapters.isEmpty()) {
            mPageChangeListener.requestChapters(chapters);
        }
    }

    @Override
    public void saveRecord() {
        super.saveRecord();
    }
}

